﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using FS.Common;

namespace FoodsSelect
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Random _random;
        FoodsInfoEntity foodsInfos = new FoodsInfoEntity();
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void foodsNum_TextChanged(object sender, EventArgs e)
        {
            if (IsInt(foodsNum.Text) && (Convert.ToInt32(foodsNum.Text) >= 1 && Convert.ToInt32(foodsNum.Text) <= 50))
            {
                return;
            }
            else
            {
                foodsNum.Text = "1";
                MessageBox.Show("请输入1~50的整数");
            }
        }
        /// <summary>
        /// 判断是否为整数
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public bool IsInt(string source)
        {
            Regex regex = new Regex(@"^(-){0,1}\d+$");
            if (regex.Match(source).Success)
            {
                if ((long.Parse(source) > 0x7fffffffL) || (long.Parse(source) < -2147483648L))
                {
                    return false;
                }
                return true;
            }
            return false;
        }
       

        private void button1_Click(object sender, EventArgs e)
        {
            string city = placeName.Text.Trim();
            if (string.IsNullOrEmpty(city))
            {
                MessageBox.Show("地点不能为空！");
                return;
            }
            string page = foodsNum.Text;
            string cacheName = city + page;
            
            if (CacheHelper.GetCache(cacheName) == null)
            {
                string getUrl = "https://restapi.amap.com/v3/place/text?keywords=美食&city=" + city + "&output=json&offset=20&page=" + page + "&key=7eee42fe046a749b422a48d17c9d4c83&extensions=all";
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(getUrl);
                myRequest.Method = "GET";
                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                string foodsInfo = reader.ReadToEnd().Replace("[]", "\"\"");
                reader.Close();
                foodsInfos = ConvertJson.DeserializeJsonToObject<FoodsInfoEntity>(foodsInfo);
                TimeSpan timeSpan = new TimeSpan(0, 0, 300);//设置缓存时间，参数分别为时分秒。
                CacheHelper.SetCache(cacheName, foodsInfos, timeSpan);
            }
            var foodsInfosObject = ConvertJson.SerializeObject(CacheHelper.GetCache(cacheName));
            foodsInfos = ConvertJson.DeserializeJsonToObject<FoodsInfoEntity>(foodsInfosObject);
            int res = GetRandomInt(0, foodsInfos.pois.Count);
            if (foodsInfos.pois != null)
            {
                foodsName.Text = foodsInfos.pois[res].name;
                
                if (foodsInfos.pois[res].photos != null)
                {
                    if (foodsInfos.pois[res].photos.Count == 1)
                    {
                        pictureBox1.ImageLocation = foodsInfos.pois[res].photos[0].url == null ? null : foodsInfos.pois[res].photos[0].url;
                    }
                    else if (foodsInfos.pois[res].photos.Count == 2)
                    {
                        pictureBox1.ImageLocation = foodsInfos.pois[res].photos[0].url == null ? null : foodsInfos.pois[res].photos[0].url;
                        pictureBox2.ImageLocation = foodsInfos.pois[res].photos[1].url == null ? null : foodsInfos.pois[res].photos[1].url;
                    }
                    else if (foodsInfos.pois[res].photos.Count == 3)
                    {
                        pictureBox1.ImageLocation = foodsInfos.pois[res].photos[0].url == null ? null : foodsInfos.pois[res].photos[0].url;
                        pictureBox2.ImageLocation = foodsInfos.pois[res].photos[1].url == null ? null : foodsInfos.pois[res].photos[1].url;
                        pictureBox3.ImageLocation = foodsInfos.pois[res].photos[2].url == null ? null : foodsInfos.pois[res].photos[2].url;
                    }
                    
                }
                else
                {
                    pictureBox1.ImageLocation = null;
                    pictureBox2.ImageLocation = null;
                    pictureBox3.ImageLocation = null;
                }
                storeAddress.Text = foodsInfos.pois[res].pname + foodsInfos.pois[res].cityname + foodsInfos.pois[res].adname + foodsInfos.pois[res].address;
                storeTag.Text = foodsInfos.pois[res].tag;
            }
            else
            {
                MessageBox.Show("当前页面无数据，请重新输入！");
            }

        }

        /// <summary>
        /// 随机一个数
        /// </summary>
        /// <param name="minNum"></param>
        /// <param name="maxNum"></param>
        /// <returns></returns>
        public int GetRandomInt(int minNum, int maxNum)
        {
            this._random = new Random();
            return this._random.Next(minNum, maxNum);
        }
    }
}
